package com.bw.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.service.OrderInfoService;
import jdk.nashorn.internal.scripts.JS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class OrderInfoController {


    @Autowired
    private OrderInfoService orderInfoService;

    @RequestMapping("/s1")
    //写接口了
    public JSONObject select1(){
        //1.声明返回值
        JSONObject result = new JSONObject();
        //2.存入需要返回的数据
        result.put("status",0);
        result.put("msg","");
        //data数据
        JSONObject data = new JSONObject();
        result.put("data",data);
        // Data中的数据存入
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();

        //存入data中
        data.put("columns",columns);
        data.put("rows",rows);

        //列数据 列的标题
        JSONObject c1 = new JSONObject();
        c1.put("id","cta");
        c1.put("name","下单金额");
        //列数据  列的标题
        JSONObject c2 = new JSONObject();
        c2.put("id","ctb");
        c2.put("name","下单次数");

        JSONObject c3 = new JSONObject();
        c3.put("id","ctc");
        c3.put("name","下单人数");

        //存入到列中
        columns.add(c1);
        columns.add(c2);
        columns.add(c3);

        //查询显示的数据
        List<Map<String, Object>> maps = orderInfoService.select1();
        for(Map<String, Object> map : maps) {
            JSONObject row = new JSONObject();
            //写死的key
            row.put("cta", map.get("cta"));
            row.put("ctb", map.get("ctb"));
            row.put("ctc",map.get("ctc"));


            rows.add(row);
        }
        return result;
    }


    @RequestMapping("/s2")
    public JSONObject select2(){
        //1。定义返回结果
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        // data数据
        JSONObject data = new JSONObject();
        //存入返回结果中
        result.put("data",data);


        //data中有columns 和rows  都是 数组
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();

        //存入columns和rows
        data.put("columns",columns);
        data.put("rows",rows);

        //第一列
        JSONObject col0 = new JSONObject();
        col0.put("id","day");
        col0.put("name","天数");

        //第二列
        JSONObject col1 = new JSONObject();
        col1.put("id","cta");
        col1.put("name","直营店优惠分摊支出金额");

       //第三列
        JSONObject col2 = new JSONObject();
        col2.put("id","ctb");
        col2.put("name","加盟店优惠分摊支出金额");

        //加入到columns
        columns.add(col0);
        columns.add(col1);
        columns.add(col2);

        List<Map<String, Object>> maps = orderInfoService.select2();
        for(Map<String,Object> map :maps){
            JSONObject row = new JSONObject();
            row.put("day",map.get("day"));
            row.put("cta",map.get("cta"));
            row.put("ctb",map.get("ctb"));
            //加入到rows中
            rows.add(row);

        }

        return result;

    }

    @RequestMapping("/s3")
    public JSONObject select3(){
        //创返回结果对象
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        //data对象
        JSONObject data = new JSONObject();
        result.put("data",data);

        //data中的categories 和 series
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();

        //加入到data中
        data.put("categories",categories);
        data.put("series",series);


        //创建一个series中的对象
        JSONObject series_row1 = new JSONObject();
        series_row1.put("name","评价次数");

        //把series_row1 加入进去
        series.add(series_row1);

        //创建一个对应的series中的data
        JSONArray series_data1 = new JSONArray();

        series_row1.put("data",series_data1);




        //创建一个series中的对象
        JSONObject series_row2 = new JSONObject();
        series_row2.put("name","好评次数");

        //把series_row1 加入进去
        series.add(series_row2);

        //创建一个对应的series中的data
        JSONArray series_data2 = new JSONArray();

        series_row2.put("data",series_data2);



        //创建一个series中的对象
        JSONObject series_row3 = new JSONObject();
        series_row3.put("name","平均评分");

        //把series_row1 加入进去
        series.add(series_row3);

        //创建一个对应的series中的data
        JSONArray series_data3 = new JSONArray();

        series_row3.put("data",series_data3);




        //创建一个series中的对象
        JSONObject series_row4 = new JSONObject();
        series_row4.put("name","好评率");

        //把series_row1 加入进去
        series.add(series_row4);

        //创建一个对应的series中的data
        JSONArray series_data4 = new JSONArray();

        series_row4.put("data",series_data4);










        //查询数据
        List<Map<String, Object>> maps = orderInfoService.select3();
        //遍历数据
        for(Map<String,Object> map :maps){
            //加入到categories中
            categories.add(map.get("name"));
            series_data1.add(map.get("cta"));
            series_data2.add(map.get("ctb"));
            series_data3.add(map.get("ctc"));
            series_data4.add(map.get("ctd"));
        }

        return result;
    }


    @RequestMapping("/s4")
    public JSONObject select4(){
        //创返回结果对象
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        //data对象
        JSONObject data = new JSONObject();
        result.put("data",data);


        //data中的categories 和 series
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();

        //加入到data中
        data.put("categories",categories);
        data.put("series",series);

        //创建一个series中的对象
        JSONObject series_row1 = new JSONObject();
        series_row1.put("name","下单金额");

        series.add(series_row1);

        JSONArray series_data1 = new JSONArray();
        series_row1.put("data",series_data1);




        //创建一个series中的对象
        JSONObject series_row2 = new JSONObject();
        series_row2.put("name","下单次数");

        series.add(series_row2);

        JSONArray series_data2 = new JSONArray();
        series_row2.put("data",series_data2);


        //创建一个series中的对象
        JSONObject series_row3 = new JSONObject();
        series_row3.put("name","下单人数");

        series.add(series_row3);

        JSONArray series_data3 = new JSONArray();
        series_row3.put("data",series_data3);



        //查询出数据
        List<Map<String, Object>> maps = orderInfoService.select4();
        for(Map<String,Object> map :maps){
            categories.add(map.get("day"));
            series_data1.add(map.get("cta"));
            series_data2.add(map.get("ctb"));
            series_data3.add(map.get("ctc"));
        }



        return result;
    }
}
